
package com.fx.apartment.modules.house.controller;

import com.fx.apartment.common.imgRecognition.baidu.IdCardRecog;
import com.fx.apartment.common.support.Condition;
import com.fx.apartment.common.support.Query;
import com.fx.apartment.common.tool.api.R;
import com.fx.apartment.common.tool.utils.Func;
import com.fx.apartment.modules.file.entity.QinFile;
import com.fx.apartment.modules.file.service.IFileService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import lombok.AllArgsConstructor;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fx.apartment.modules.house.entity.IntoHouse;
import com.fx.apartment.modules.house.vo.IntoHouseVO;
import com.fx.apartment.modules.house.wrapper.IntoHouseWrapper;
import com.fx.apartment.modules.house.service.IIntoHouseService;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 入住管理 控制器
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/intohouse")
@Api(value = "入住管理", tags = "入住管理接口")
public class IntoHouseController{

	private final IIntoHouseService intoHouseService;

	private final IdCardRecog idCardRecog;

	private final IFileService fileService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入intoHouse")
	public R<IntoHouseVO> detail(IntoHouse intoHouse) {
		IntoHouse detail = intoHouseService.getOne(Condition.getQueryWrapper(intoHouse));
		return R.data(IntoHouseWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 入住管理
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入intoHouse")
	public R<IPage<IntoHouseVO>> list(IntoHouse intoHouse, Query query) {
		IPage<IntoHouse> pages = intoHouseService.page(Condition.getPage(query), Condition.getQueryWrapper(intoHouse));
		return R.data(IntoHouseWrapper.build().pageVO(pages));
	}


	/**
	 * 新增 入住管理
	 */
	@PostMapping("/apply")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "入住申请", notes = "传入intoHouse")
	public R apply(@Valid @RequestBody IntoHouse intoHouse) {
		return R.data(intoHouseService.apply(intoHouse));
	}

	/**
	 * 审核
	 */
	@PostMapping("/audit")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "审核申请", notes = "传入intoHouse")
	public R audit(@NotNull(message = "id 不能为空") @RequestParam("id") long id,
				   @NotNull(message = "status 不能为空") @RequestParam("status") Integer status,
				   @RequestParam("message") String message) {

		return R.status(intoHouseService.audit(id,status,message));
	}

	/**
	 * 删除 入住管理
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(intoHouseService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 身份证识别
	 */
	@PostMapping("/identifying")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "身份证识别", notes = "传入intoHouse")
	public R apply(@ApiParam(value = "类型front正面back反面", required = true)@NotEmpty @RequestParam("type") String type,
				   @ApiParam(value = "文件", required = true)@NotNull @RequestParam("file") MultipartFile file) {
		QinFile qinFile = fileService.putFile(file);
		Map<String, Object> map = idCardRecog.discriminate4Map(qinFile.getOriginalName(), type);
		return R.data(map);
	}
	
}
